<?php
/**
 *  date: 2017/7/10  11:11
 *  Author: L先生
 *  encoding:utf-8  language:PHP
 */

namespace Admin\Model;


class ServeModel extends AdminBaseModel
{
    protected $tableName = 'answer_formalinfo';

    /**
     *申请列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function getproposerList($name,$mobile,$status,$type)
    {
        if($name)
            $map['facilitator_name'] = array('like','%'.$name.'%');
        if ($mobile)
            $map['mobile'] = array('like','%'.$mobile.'%');
        if ($status)
            $map['status'] = $status;
        if ($type)
            $map['type'] = $type;
        $fqModel = M('facilitator_proposer');
        $count =$fqModel->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page -> show();
        $result = $fqModel->alias('fp')
                        ->where($map)
                        ->field('fp.*,u.nickname')
                        ->join('LEFT JOIN __USERS__ u  ON u.id=fp.user_id ')
                        ->limit($Page->firstRow.','.$Page->listRows)
                        ->order('id desc')
                        ->select();
        return array($result,$show);
    }


    /**
     *酒店列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function gethotelList($name,$mobile,$status,$type)
    {
        if($name)
            $map['hotel_name'] = array('like','%'.$name.'%');
        if ($mobile)
            $map['mobile'] = array('like','%'.$mobile.'%');
        if ($status)
            $map['status'] = $status;
        if ($type)
            $map['type'] = $type;
        $hModel = M('hotel');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page -> show();
        $result = $hModel->alias('h')
            ->where($map)
            ->field('h.*,u.nickname')
            ->join('LEFT JOIN __USERS__ u  ON u.id=h.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        return array($result,$show);
    }


    /**
     *交通列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function gettrafficList($name,$mobile,$status)
    {
        if($name)
            $map['traffic_name'] = array('like','%'.$name.'%');
        if ($mobile)
            $map['mobile'] = array('like','%'.$mobile.'%');
        if ($status)
            $map['status'] = $status;
        $hModel = M('traffic');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page -> show();
        $result = $hModel->alias('t')
            ->where($map)
            ->field('t.*,u.nickname')
            ->join('LEFT JOIN __USERS__ u  ON u.id=t.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        return array($result,$show);
    }


    /**
     *交通列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function getbaseList($name,$mobile,$status)
    {
        if($name)
            $map['base_name'] = array('like','%'.$name.'%');
        if ($mobile)
            $map['mobile'] = array('like','%'.$mobile.'%');
        if ($status)
            $map['status'] = $status;
        $hModel = M('base');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page -> show();
        $result = $hModel->alias('t')
            ->where($map)
            ->field('t.*,u.nickname')
            ->join('LEFT JOIN __USERS__ u  ON u.id=t.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        return array($result,$show);
    }


    public function getUserHotelList($name,$mobile)
    {

        if ($mobile)
            $map['mt_user_hotel.mobile'] = array('like','%'.$mobile.'%');

        $hModel = M('user_hotel');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page -> show();
        $result = $hModel
            ->where($map)
            ->field('mt_user_hotel.*,mt_users.nickname,mt_hotel.hotel_name')
            ->join('LEFT JOIN mt_users   ON mt_users.id=mt_user_hotel.user_id ')
            ->join('LEFT JOIN mt_hotel   ON mt_hotel.id=mt_user_hotel.hotel_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        return array($result,$show);
    }


    public function getUserBaselList($mobile)
    {
        if ($mobile)
            $map['mt_user_base.mobile'] = array('like','%'.$mobile.'%');
        $hModel = M('user_base');
        $count =$hModel->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page -> show();
        $result = $hModel
            ->where($map)
            ->field('mt_user_base.*,mt_users.nickname,mt_base.base_name')
            ->join('LEFT JOIN mt_users   ON mt_users.id=mt_user_base.user_id ')
            ->join('LEFT JOIN mt_base   ON mt_base.id=mt_user_base.base_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        return array($result,$show);
    }









    public function deleteAllQuestion($idArr)
    {
        foreach($idArr['id'] as $vo)
        {
            M('question_bank')->where(array('id'=>$vo))->delete();
        }
        return true;
    }

    /**
     *答题信息
     * @Date 201707
     * @Author Lin
     */
    public function getAnswerInfoList($username,$school,$area,$city)
    {
        $map = array();
        if($username){
            $map['mt_answer_formalinfo.user_id'] = M('users')->where(array('nickname'=>array('like','%'.$username.'%')))->getField('id');
        }
        if($school)
            $map['mt_answer_formalinfo.school'] = array('like','%'.$school.'%');
        if($area)
            $map['mt_answer_formalinfo.area'] = array('like','%'.$area.'%');
        if($city)
            $map['mt_answer_formalinfo.city'] =array('like','%'.$city.'%');

        $order = array('score'=>'desc','total_time'=>'asc','id'=>'desc');

        $count = $this->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page->show();

        $result = $this
            ->field('mt_answer_formalinfo.*,mt_users.nickname')
            ->where($map)
            ->join('LEFT JOIN mt_users  ON mt_users.id=mt_answer_formalinfo.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($order)
            ->select();
        return array($result,$show);
    }

    /**
     *全选删除信息
     * @param $idArr
     * @return bool
     * @Date 201707
     * @Author Lin
     */
    public function deleteAllData($idArr)
    {
        $model = M($idArr['model']);
        foreach($idArr['id'] as $vo)
            $model->where(array('id'=>$vo))->delete();
        return true;
    }




    /**
     * 获取分类列表
     */
    public function getCategory($name){
        if($name){
            $map['name'] = array('like','%'.$name.'%');
        }
        $count = M('base_category') -> where($map)->count();// 查询满足要求的总记录
        $Page = new \Think\Page($count, 15);
        $show = $Page->show();
        $model = M('base_category');
        $result = $model->where($map)->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();
        return array($result, $show);
    }

    /**
     * 获取分类字段
     */
    public function getCategoryField(){
        $model = M('base_category');
        return $model->field('id, name')->where("is_show = 1")->order('sort asc, id desc')->select();
    }

    /**
     * 获取分类名称
     */
    public function getCategoryName($id){
        $model = M('base_category');
        return $model->where("id = $id")->getField('name');
    }

    /**
     * 获取一条分类
     */
    public function getCategoryFind($id) {
        $model = M('base_category');
        return $model->where("id = $id")->find();
    }

    /**
     * 修改分类
     */
    public function editCategory($id, $data) {
        $admin_base_model = new AdminBaseModel('base_category');
        return $admin_base_model->editData(array('id'=>$id), $data);
    }

    /**
     * 删除分类
     */
    public function deleteCategory($id) {
        $admin_base_model = new AdminBaseModel('base_category');
        return $admin_base_model->deleteData(array('id'=>$id));
    }

    /**
     * 全选删除分类
     */
    public function deleteAllCategory($idArr) {
        $model = M('base_category');
        foreach ($idArr['category_id'] as $k => $v){
            $model->where("id = $v")->delete();
        }
        return true;
    }

}